//mazeBlock.h的实现文件
#include <QPainter>
#include <QBrush>
#include "mazeBlock.h"
#include "mazeBoard.h"

mazeBlock::mazeBlock(QWidget *parent) : QWidget(parent)
{
    showState = 0;
    buildState = 0;
    pathState = 0;
    this->setFixedSize(35, 35);         //设置方格的初始大小为35X35
    blockColor.setRgb(50, 50, 50);
    this->update();

}


void mazeBlock::paintEvent(QPaintEvent *event)
{
    mazeBoard* tmpBoard = (mazeBoard*) board;       //获取方格所属的迷宫
    if(tmpBoard->getBoardModal() == 1)      //显示模式
    {
        if(showState == 0)                  //断路
        {
            blockColor.setRgb(50,50,50);    //深灰色
        }
        else if(pathState == 1)             //通路且是路径
        {
            blockColor.setRgb(250,100,100); //粉红色
        }
        else
        {                                   //通路且不是路径
            blockColor.setRgb(200,200,200); //浅灰色
        }
    }
    else if(tmpBoard->getBoardModal() == 0) //建筑模式
    {
        if(buildState == 1)                 //通路
        {
            blockColor.setRgb(200,200,200); //浅灰色
        }
        else if(buildState == 0)            //断路
        {
            blockColor.setRgb(50,50,50);    //深灰色
        }
    }
    QPainter painter(this);

    if(tmpBoard)

    painter.setBrush(QBrush(blockColor));
    painter.drawRect(this->rect());
}


int mazeBlock::setBoard(QWidget *b)
{
    board = b;
    return 1;
}

int mazeBlock::getBuildState()
{
    return buildState;
}

int mazeBlock::setShowState(int s)
{
    showState = s;
    this->update();
    return 1;
}

int mazeBlock::setPathState(int s)
{
    pathState = s;
    this->update();
    return 1;
}

void mazeBlock::mousePressEvent(QMouseEvent *event)
{
    if(buildState == 1)
    {
        buildState = 0;
    }
    else
    {
        buildState = 1;
    }
    this->update();
}


int mazeBlock::setBuildState(int s)
{
    buildState = s;
    return 1;
}
